using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using RukouMVC.Models;
using RukouMVC.Helpers;

namespace RukouMVC.Controllers
{
    public class Messages
    {
        public List<Message> MessageList { get; set; }
    }


    public class MessageController : BaseController
    {
        Messages messages = new Messages();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult One2One(string id,int? page)
        {
            string[] ids = id.Split('-');
            int userId1 = int.Parse(ids[0]) - 1234;
            int userId2 = int.Parse(ids[1]) - 1234;
            ViewData["user1"] = (rdc.Users.Single(u => u.UserId == userId1)).DisplayName;
            ViewData["user2"] = (rdc.Users.Single(u => u.UserId == userId2)).DisplayName;

            const int pageSize = 10;

            var query = from m in rdc.Messages
                        where
                            (m.SenderId == userId1 && m.ReceiverId == userId2)
                            || (m.SenderId == userId2 && m.ReceiverId == userId1)
                        orderby m.CreatedAt descending
                        select m;
            var paginatedMessages = new PaginatedList<Message>(query, page ?? 0, pageSize);

            

            //messages.MessageList = query.ToList<Message>();

            return View(paginatedMessages);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult One2One(string id,FormCollection collection)
        {
            string[] ids = id.Split('-');
            int userId1 = int.Parse(ids[0]) - 1234;
            int userId2 = int.Parse(ids[1]) - 1234;
            ViewData["user1"] = (rdc.Users.Single(u => u.UserId == userId1)).DisplayName;
            ViewData["user2"] = (rdc.Users.Single(u => u.UserId == userId2)).DisplayName;

            Message message = new Message();
            message.Content = collection["messagecontent"].Replace("\r\n","<br/>");
            message.SenderId = userId1;
            message.SenderName = ViewData["user1"].ToString();
            message.ReceiverId = userId2;
            message.ReceiverName = ViewData["user2"].ToString();
            message.HasRead = false;
            message.CreatedAt = DateTime.Now;

            Pal pal = rdc.Pals.SingleOrDefault(p => p.ContactId == userId1 && p.BeContactedId == userId2);
            if (pal != null)
            {
                pal.UpdatedAt = DateTime.Now;
                rdc.SubmitChanges();
            }
            else 
            {
                pal = new Pal();
                pal.ContactId = userId1;
                pal.BeContactedId = userId2;
                pal.CreatedAt = DateTime.Now;
                pal.UpdatedAt = DateTime.Now;
                rdc.Pals.InsertOnSubmit(pal);
                rdc.SubmitChanges();
            }

            rdc.Messages.InsertOnSubmit(message);
            rdc.SubmitChanges();

            return RedirectToAction("One2One");
        }


        public ActionResult Pals()
        {
            ViewData["UserId"] = cookie["UserId"];

            var query = from u in rdc.Users
                        join p in rdc.Pals on u.UserId equals p.BeContactedId
                        where p.ContactId == int.Parse(cookie["UserId"])
                        select u;

            var users = query.AsEnumerable<User>();

            return View(users);
        }

    }
}
